package com.sskj.wallet.ui.activity;


import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AddressBean;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.ScanBean;
import com.sskj.lib.model.room.AddressBeanViewModel;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.AddressEditActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.WALLET_ADDRESS_EDIT)//地址编辑
public class AddressEditActivity extends BaseActivity<AddressEditActivityPresenter> {
    @BindView(R2.id.etMemo)
    EditText etMemo;
    @BindView(R2.id.ivCoin)
    ImageView ivCoin;
    @BindView(R2.id.llChoose)
    LinearLayout llChoose;
    @BindView(R2.id.etAddress)
    EditText etAddress;
    @BindView(R2.id.ivScan)
    ImageView ivScan;
    @BindView(R2.id.etPhone)
    EditText etPhone;
    @Inject
    AddressBeanViewModel addressBeanViewModel;
    @Autowired
    AddressBean addressBean;
    private AndroidNextInputs inputs;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_address_edit;
    }

    @Override
    public AddressEditActivityPresenter getPresenter() {
        return new AddressEditActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.wallet_addressEditActivity1_j));
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        DaggerUserDataComponent.create().inject(this);
        updateShow();
        ClickUtil.click(ivScan, () -> {
            ARouter.getInstance().build(RConfig.LIB_SCAN).navigation();
        });
        inputs = new AndroidNextInputs();
        WidgetAccess widgetAccess = new WidgetAccess(this);
        inputs
                .add(widgetAccess.findEditText(R.id.etMemo), StaticScheme.required())
                .add(widgetAccess.findEditText(R.id.etAddress), StaticScheme.required())
        ;
        etPhone.setText(addressBean.getPhone());
        etMemo.setText(addressBean.getNotes());
        etAddress.setText(addressBean.getAddress());
        ivCoin.setImageResource(CoinEnum.getByCode(addressBean.getCode()).getRes());
    }

    private void updateShow() {
        setRightTitle(App.INSTANCE.getString(R.string.wallet_addressEditActivity2_j), v -> {
            etAddress.setEnabled(true);
            etMemo.setEnabled(true);
            etPhone.setEnabled(true);
            updateEdit();
        });
    }

    private void updateEdit() {
        setRightTitle(App.INSTANCE.getString(R.string.wallet_addAddressActivity2_j), v -> {
            if (inputs.test()) {
                etAddress.setEnabled(false);
                etMemo.setEnabled(false);
                etPhone.setEnabled(false);
                updateShow();
                addressBean.setAddress(etAddress.getText().toString());
                addressBean.setNotes(etMemo.getText().toString());
                addressBean.setPhone(etPhone.getText().toString());
                addressBeanViewModel.insert(addressBean);
                ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_addAddressActivity3_j));
            }

        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void scanBack(ScanBean scanBean){
        etAddress.setText(scanBean.getContent());
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }


}
